import { Ref } from "vue";

export default {
    //库中是否有相同值

    /**
     * 验证一个字段在数据库是否存在
     * 先在使用页面中定义一个 const oldLoginName = ref(null); 变量名随便起
     * 然后在获取数据的地方，一般是Init方法里的GetByID接口中，在获取到数据后，给这个变量赋值
     * 在rules中，对应的地方增加 { validator: Validators.DBHas("User").bind(null, oldLoginName), trigger: "blur" }
     * 说明：通过.bind中绑定的 oldLoginName ，就会传到oldValue中，所以oldValue也要定义为Ref；这里使用的oldValue主要是为了在处理修改功能时，如果值不一样，不能判断为重复
     * @param tableName 表名
     * @param tips 提示字段名称
     * @param field 查询验证的filterModel
     * @returns
     */
    DBHas(tableName: string, tips: string = "名称", field: string = "_NameEqual") {
        if (Gx.IsEmpty(tableName)) {
            throw new Error("未提供相应参数！");
        }

        tips = `${tips}已经存在，请重新输入`;

        return function (oldValue: Ref<any>, rule: any, value: any, callback: any) {
            if (Gx.IsEmpty(value)) {
                return callback();
            }

            if (!Gx.IsEmpty(oldValue) && !Gx.IsEmpty(oldValue.value) && oldValue.value == value) {
                return callback();
            }

            App.CallApi(Api[tableName].GetCount, {
                data: {
                    [field]: value,
                },
                loading: false,
                success: (res: number) => {
                    if (res > 0) {
                        callback(new Error(tips));
                    } else {
                        callback();
                    }
                },
            });
        };
    },

    //整数
    IsInt(tips: string = "请输入整数") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsInt(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //正整数 x>0
    IsIntPositive(tips: string = "请输入大于0的整数") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsIntPositive(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //负整数 x<0
    IsIntNegative(tips: string = "请输入小于0的整数") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsIntNegative(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //正整数 x>=0
    IsIntPositiveByZero(tips: string = "请输入大于等于0的整数") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsIntPositiveByZero(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //负整数 x<=0
    IsIntNegativeByZero(tips: string = "请输入小于等于0的整数") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsIntNegativeByZero(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //数字
    IsNumber(tips: string = "请输入数字") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsNumber(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //正数字 x>0
    IsNumberPositive(tips: string = "请输入大于0的数字") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsNumberPositive(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //负数字 x<0
    IsNumberNegative(tips: string = "请输入小于0的数字") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsNumberNegative(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //正数字 x>=0
    IsNumberPositiveByZero(tips: string = "请输入大于等于0的数字") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsNumberPositiveByZero(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //负数字 x<=0
    IsNumberNegativeByZero(tips: string = "请输入小于等于0的数字") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsNumberNegativeByZero(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //验证小数位必需要有几位，不能没有小数位
    HasPrecision(n: number = 2, tips: string = `必需是${n}位小数`) {
        return function (rule: any, value: any, callback: any) {
            if (Gx.HasPrecision(value, n)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //验证小数位不能小于且大于几位，可以没有小数位
    MaxPrecision(max: number = 2, min: number = 1, tips: string = `小数位应在${min}~${max}位之间`) {
        return function (rule: any, value: any, callback: any) {
            if (Gx.MaxPrecision(value, max, min)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //自定义正则
    RegexMatch(reg: any, tips: string) {
        return function (rule: any, value: any, callback: any) {
            if (Gx.RegexMatch(reg, value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //邮箱
    IsEmail(tips: string = "邮箱格式错误") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsEmail(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //手机号
    IsMobile(tips: string = "手机号格式错误") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsMobile(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //固定电话号
    IsTel(tips: string = "电话号格式错误") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsTel(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //联系电话  手机+固话
    IsPhone(tips: string = "手机或电话号格式错误") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsPhone(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //IP地址
    IsIp(tips: string = "IP地址格式错误") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsIp(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //中文
    IsChinese(tips: string = "请输入中文") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsChinese(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //字母
    IsLetter(tips: string = "请输入字母") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsLetter(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },

    //字母与数字
    IsLetterNumber(tips: string = "请输入字母或数字") {
        return function (rule: any, value: any, callback: any) {
            if (Gx.IsLetterNumber(value)) {
                callback();
            } else {
                callback(new Error(tips));
            }
        };
    },
};
